<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员登录 - 有偿知识问答系统</title>
    <link rel="stylesheet" href="/knowledge-qa/static/css/style.css">
    <link rel="stylesheet" href="/knowledge-qa/static/css/modern_style.css">
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
        }
        .login-container {
            background-color: white;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            width: 350px;
            text-align: center;
        }
        .login-title {
            font-size: 24px;
            margin-bottom: 30px;
            color: #333;
        }
        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #666;
        }
        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
        .btn-primary {
            width: 100%;
            padding: 12px;
            background-color: #1890ff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            margin-top: 10px;
        }
        .btn-primary:hover {
            background-color: #40a9ff;
        }
        .alert {
            padding: 10px;
            margin-bottom: 15px;
            border-radius: 4px;
            display: none;
        }
        .alert-danger {
            background-color: #fff1f0;
            color: #ff4d4f;
            border: 1px solid #ffccc7;
        }
        .system-name {
            font-size: 18px;
            color: #999;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2 class="login-title">管理员登录</h2>
        <div id="loginMessage" class="alert alert-danger"></div>
        <form id="adminLoginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" placeholder="请输入管理员用户名">
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入管理员密码">
            </div>
            <button type="button" class="btn-primary" onclick="adminLogin()">登录</button>
        </form>
        <div class="system-name">有偿知识问答系统管理后台</div>
    </div>
    
    <script>
        // 显示消息
        function showMessage(message, isSuccess) {
            const messageElement = document.getElementById('loginMessage');
            if (messageElement) {
                messageElement.textContent = message;
                messageElement.className = 'alert alert-danger';
                messageElement.style.display = 'block';
                
                // 3秒后自动隐藏
                setTimeout(() => {
                    messageElement.style.display = 'none';
                }, 3000);
            }
        }
        
        // 管理员登录
        function adminLogin() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            if (!username || !password) {
                showMessage('请输入用户名和密码', false);
                return;
            }
            
            // 发送登录请求
            fetch('/knowledge-qa/api/admin/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({username, password})
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 200) {
                    // 保存管理员信息到localStorage
                    localStorage.setItem('adminId', data.data.adminId);
                    localStorage.setItem('adminUsername', data.data.adminUsername);
                    localStorage.setItem('isAdmin', true);
                    
                    // 跳转到管理员后台首页
                    window.location.href = '/knowledge-qa/static/pages/admin_dashboard.html';
                } else {
                    showMessage(data.message || '登录失败', false);
                }
            })
            .catch(error => {
                console.error('登录失败:', error);
                showMessage('登录失败，请稍后重试', false);
            });
        }
        
        // 监听回车键
        document.getElementById('adminLoginForm').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                adminLogin();
            }
        });
    </script>
</body>
</html>